//
//  STTabsViewController.swift
//  iPhone
//
//  Created by Toj on 11/15/23.
//

import UIKit

protocol STTabViewScrollDelegate: NSObjectProtocol {
    func tabView(_ tabView: STTabViewProtocol, scrollView offsetY: CGFloat)
}
extension STTabViewScrollDelegate {
    func tabView(_ tabViewTarget: STTabViewProtocol, scrollView offsetY: CGFloat) { }
}

protocol STTabViewProtocol { }
extension UIView: STTabViewProtocol { }
extension UIViewController: STTabViewProtocol { }
 
protocol STTabViewTargetDelegate where Self: STTabViewProtocol {
    var scrollDelegate: STTabViewScrollDelegate? { get set }
    var contentOffset: CGPoint { get set }
    
    var scrollView: UIScrollView? { get }
}

extension STTabViewTargetDelegate {
    
    var scrollView: UIScrollView? { return nil }
}


protocol STTabsViewControllerDelegate: NSObjectProtocol {
    func tabsViewController(_ viewController: STTabsViewController, scrollView offsetY: CGFloat)
}

// MARK: - 懒加载(属性), 变量, 初始化init
class STTabsViewController: UIViewController {
    
    weak var tdelegate: STTabsViewControllerDelegate?
    
    var selectedIndex: Int = -1
    
    var tabs: [STTabViewModel] = [] { didSet {
        let isShowTabs = tabs.count > 1
        tabsView.isHidden = !isShowTabs
        setupMakeRelayoutSubviews()
        guard isShowTabs else { return }
        
        tabsView.tabs = tabs
        setChildView()
    } }
    
    /** <#NOTE#> **/
    private let tabViewHeight: CGFloat = 40
    private lazy var tabsView: STTabView = {
        let view = STTabView()
        view.backgroundColor = .white
        view.delegate = self
        return view
    }()
    
    private(set) lazy var contentView: UIView = {
        let view = UIView()
        return view
    }()
    
    deinit {
        print("-->startimes: \(st_className) func: deinit")
    }
}

// MARK: - 生命周期
extension STTabsViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension STTabsViewController {
    
}

// MARK: - Public
extension STTabsViewController {
    
    func reloadTabs() {
        tabsView.reloadTabs()
    }
}

// MARK: - Private
private extension STTabsViewController {
    func setChildView() {
        contentView.subviews.forEach { view in
            view.removeFromSuperview()
        }
        
        guard tabs.count > selectedIndex else { return }
        
        var detailView: UIView?
        let viewModel = tabs[selectedIndex]
        if let childViewController = viewModel.childViewController as? UIViewController {
            detailView = childViewController.view
        }
        else if let childView = viewModel.childView as? UIView {
            detailView = childView
        }
        viewModel.childViewController?.scrollDelegate = self
        
        guard let dview = detailView else { return  }
        contentView.backgroundColor = .random
        contentView.addSubview(dview)
        dview.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
}

// MARK: - STTabViewScrollDelegate
extension STTabsViewController: STTabViewScrollDelegate {
    
    func tabView(_ tabView: STTabViewProtocol, scrollView offsetY: CGFloat) {
        guard !tabsView.isHidden else { return }
        tdelegate?.tabsViewController(self, scrollView: offsetY)
    }
}

extension STTabsViewController: STTabViewDelegate {
    
    func tabView(_ tabView: STTabView, didSelected tab: STTabViewModel) {
        let index = tab.index
        guard index < tabs.count else { return }
        selectedIndex = index
        setChildView()
    }
}

// MARK: - 初始化设置
private extension STTabsViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(contentView)
        view.addSubview(tabsView)
    }
    
    func setupMakeLayoutSubviews() {
        tabsView.snp.makeConstraints { make in
            make.top.left.right.equalTo(view)
            make.height.equalTo(tabViewHeight)
        }
        contentView.snp.makeConstraints { make in
            make.edges.equalTo(view)
        }
    }
    
    func setupMakeRelayoutSubviews() {
        let top: CGFloat = tabsView.isHidden ? 0 : tabViewHeight
        print("-->startimes: top: \(top)")
        contentView.snp.remakeConstraints { make in
            make.top.equalTo(top)
            make.bottom.left.right.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        
    }
}
